package com.dawnClass.controller;

import com.dawnClass.pojo.Consumer;
import com.dawnClass.service.CourseService;
import com.dawnClass.service.LearningRateService;
import com.dawnClass.util.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private LearningRateService learningRateService;

    @GetMapping("/course")
    @ResponseBody
    public ModelAndView toCourse(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("course/course");
        return mav;
    }

//    异步分页获取课程页面首页课程数据
    @GetMapping("/getCourse/{pageNum}")
    @ResponseBody
    public ServiceResponse getCourse(@PathVariable("pageNum") Integer pageNum ,
                                     String a ,
                                     String b ,
                                     String c) {
        return courseService.getAllCourse(pageNum , a , b , c);
    }

//    获取课程详情数据
    @GetMapping("/learn/{courseId}")
    @ResponseBody
    public ModelAndView getACourse(@PathVariable("courseId") Integer courseId , HttpSession session){

        //获取session中存储的当前用户数据
        ServiceResponse consumerResponse = (ServiceResponse)session.getAttribute("ServiceResponse");
        Consumer consumer = (Consumer)consumerResponse.getdata();

        ModelAndView mav = new ModelAndView();

        //课程信息
        ServiceResponse courseInfo = courseService.getCourseByCourseId(courseId);

        //章节信息
        ServiceResponse chapterInfo = courseService.getChapterInfo(consumer.getConsumerId(),courseId);

        //课程评价
        ServiceResponse reviewInfo = courseService.getReviewByCourseId(courseId);

        //用户是否已经购买该课程
        ServiceResponse purchasedState = courseService.getCoursePurchaseStatus(consumer.getConsumerId() , courseId);

        //课程最后学习节点
        ServiceResponse courseRate = learningRateService.getCourseRate(consumer.getConsumerId(), courseId);

        System.out.println(purchasedState);
        //绑定页面数据
        mav.addObject("courseRate",courseRate);
        mav.addObject("purchasedState",purchasedState);
        mav.addObject("courseInfo",courseInfo);
        mav.addObject("chapterInfo",chapterInfo);
        mav.addObject("reviewInfo",reviewInfo);

        //绑定跳转页面
        mav.setViewName("learn/learn");
        return mav;
    }

    //模糊查询
    @GetMapping("/getCourseName/{courseName}")
    @ResponseBody
    public ServiceResponse getCourseLike(@PathVariable("courseName") String courseName ,
                                     String a ,
                                     String b ,
                                     String c) {
        return courseService.getAllCourseLike(courseName , a , b , c);
    }
}
